﻿$(function() {
    // Document.ready -> link up remove event handler 
    $(".RemoveLink").click(function() {
        // Get the id from the link 
        var recordToDelete = $(this).attr("data-id");

        if (recordToDelete != '') {
            // Perform the ajax post 
            $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
                    function(data) {
                        // Successful requests get here 
                        // Update the page elements 
                        $('#row-' + data.DeleteId).fadeOut('slow');
                        $('#update-message').text(data.Message);
                        $('#cart-total').text(data.CartTotal);
                        $('#cart-status').text('Giỏ hàng (' + data.CartCount + ')');
                    });
        }
    });
});
//$(function() {
//    // Document.ready -> link up remove event handler
//    $(".RemoveLink1").click(function() {
//        // Get the id from the link
//        var recordToDelete = $(this).attr("data-id");

//        if (recordToDelete != '') {

//            // Perform the ajax post
//            $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
//                    function(data) {
//                        // Successful requests get here
//                        // Update the page elements
//                        if (data.ItemCount == 0) {
//                            $('#row-' + data.DeleteId).fadeOut('slow');
//                        } else {
//                            $('#item-count-' + data.DeleteId).text(data.ItemCount);
//                        }

//                        $('#cart-total').text(data.CartTotal);
//                        $('#update-message').text(data.Message);
//                        $('#cart-status').text('Giỏ hàng (' + data.CartCount + ')');
//                    });
//        }
//    });

//});


//function handleUpdate() {
//    // Load and deserialize the returned JSON data
//    var json = context.get_data();
//    var data = Sys.Serialization.JavaScriptSerializer.deserialize(json);

//    // Update the page elements
//    if (data.ItemCount == 0) {
//        $('#row-' + data.DeleteId).fadeOut('slow');
//    } else {
//        $('#item-count-' + data.DeleteId).text(data.ItemCount);
//    }

//    $('#cart-total').text(data.CartTotal);
//    $('#update-message').text(data.Message);
//    $('#cart-status').text('Giỏ hàng (' + data.CartCount + ')');
//}

function CheckQuantity(qty_box) {
    var qty = parseInt(qty_box.value);
    // Get the id from the link
    var recordToCheck = $(qty_box).parent().attr("data-id");
    
    //qty=0, remove from cart
    if (qty <= 0) {
        if (recordToCheck != '') {
            $.post("/ShoppingCart/RemoveFromCart", { "id": recordToCheck },
                function(data) {
                    // Successful requests get here 
                    // Update the page elements 
                    $('#row-' + data.DeleteId).fadeOut('slow');
                    $('#update-message').text(data.Message);
                });
        }
    }
    else if (isNaN($("#item-count-" + recordToCheck).val()) || isNaN(qty)) {
        qty = 1;
        if (recordToCheck != '') {
            $.post("/ShoppingCart/ChangeQuantity", { "id": recordToCheck, "quantity": qty },
                function(data) {
                    // Successful requests get here 
                    // Update the page elements
                    $('#item-count-' + recordToCheck).val(data.ItemCount);
                    $('#cart-total').html(data.CartTotal);
                    $('#cart-status').html('Giỏ hàng (' + data.CartCount + ')');
                });
        }
    }
}

function ChangeQuantity(qty_box) {
    var qty = parseInt(qty_box.value);
    // Get the id from the link
    var recordToChange = $(qty_box).parent().attr("data-id");

    if (qty < 0 || qty == "") {
        qty = 0;
    }

    if (isNaN($("#item-count-" + recordToChange).val()) || isNaN(qty)) {
        return;
    }
    
    if (recordToChange != '') {
        // Perform the ajax post
        $.post("/ShoppingCart/ChangeQuantity", { "id": recordToChange, "quantity": qty },
                    function(data) {
                        // Successful requests get here
                        // Update the page elements
                        //$('#item-count-' + recordToChange).val(data.ItemCount);
                        $('#cart-total').html(data.CartTotal);
                        $('#cart-status').html('Giỏ hàng (' + data.CartCount + ')');
                    });
    }
    
}

$(function() {
    $('#searchTermAdvance').focus(function() {
        if ($(this).val() == 'Từ khóa') {
            $(this).val('').css('font-style', 'normal');
        }
    });

    $('#searchTermAdvance').blur(function() {
        if ($(this).val() == '') {
            $(this).val('Từ khóa').css('font-style', 'italic');
        }
    });
});

//$(document).ready(function() {
//    $('#searchSimpleButton').click(function() {
//        $('#divControlTemp').load('Home/Search');
//    });
//});

$(window).scroll(function() {
    if ($(window).scrollTop() + $(window).height() >= $(document).height() - 100) {
        var bottom = 100 - ($(document).height() - ($(window).scrollTop() + $(window).height())) + "px";
        $('#categories').css({ 'bottom': bottom });
    } 
    else {
        $('#categories').css({ 'bottom': '' });
    }
});

        
